package com.aos.erp.common.utils;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * @Auther: 杨文博
 * @Date: 2018/10/31 18:12
 * @Description:
 */
public class ValidationUtil {
	private static Validator validator;

	static {
		ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
		validator = vf.getValidator();
	}

	public static <T> void validate(T t) throws ValidationException {
		Set<ConstraintViolation<T>> set = validator.validate(t);
		if (set.size() > 0) {
			StringBuilder validateError = new StringBuilder();
			for (ConstraintViolation<T> val : set) {
				validateError.append(val.getMessage() + " ;");
			}
			throw new ValidationException(validateError.toString());
		}
	}
}
